﻿using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System;

namespace Pitaya_IDE
{

    public class FileMunePanel : Panel
    {
        private bool show1 = true;
        public FileMune fileMune;                                   //文件目录
        public ScrollBar scrollBar;

        public Form1 form1 {
            set {
                fileMune.form1 = value;
                scrollBar.form1 = value;
            }
        }

        public FileMunePanel()
        {
            fileMune = new FileMune
            {
                Top = 0,
                Left = 0,
                Width = Width,
                Height = Height,
            };

            

            MouseWheel += new MouseEventHandler(FileMunePanel_MouseWheel);

            scrollBar = new ScrollBar
            {
                Width = 15,
                Visible = true,
                vMax = 1000,
                vMin = 0,
                sP = 0,
                eP = Height,
                HV = false,
                panel = fileMune,
                Stealth = true,
            };
            Controls.Add(scrollBar);
            Controls.Add(fileMune);
        }

        public void Time_Stealth()
        {
            scrollBar.Time_Stealth();
        }

        public void flushed()
        {
            scrollBar.vMax = (fileMune.FolderAndFileNumOld * 32);
            scrollBar.eP = Height;
            scrollBar.Left = Width - 17;
            scrollBar.Now_value = -fileMune.Top;
        }

        public void Draw()
        {
            fileMune.Width = Width;
            
            fileMune.Draw();
            flushed();
        }

        public void SetupNetDisk(string sndisk)
        {
            fileMune.BackColor = BackColor;
            fileMune.SetupNetDisk(sndisk);
        }

        public bool Show1
        {
            get => show1;
            set
            {
                show1 = value;
                fileMune.Show1 = value;
            }
        }

        private void FileMunePanel_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0 && fileMune.Top >= 0)
            {
                fileMune.Top = 0;
                flushed();
                return;
            }

            if (fileMune.Height + fileMune.Top <= Height && e.Delta < 0)
            {
                flushed();
                return;
            }

            fileMune.Top += e.Delta / 120 * 32;
            flushed();
        }
    }
    
    
    
    public class FileMune:Panel
    {
        private bool show1 = true;                                  //是否显示
        private string NetDisk = null;                              //驱动器路径
        public List<string> ExpandedFolder = new List<string>();    //展开的目录
        public Form1 form1;                                         //主窗体句柄
        public bool git_support=false;                              //git支持

        public bool Show1 
        {   get => show1;
            set { 
                show1 = value;
            }
        }

        public void SetupNetDisk(string sndisk)
        {
            NetDisk = sndisk;
            
        }

        public int FolderAndFileNum = 0, FolderAndFileNumOld=0;

        public void Draw()
        {
            DirectoryInfo dir = new DirectoryInfo(NetDisk);
            DirectoryInfo[] subDirs = dir.GetDirectories(); //获取所有子文件夹

            foreach (DirectoryInfo subDir in subDirs)//文件夹
            {
                if (subDir.Name.Equals(".git"))
                {
                    git_support = true;
                    break;
                }
            }

            FolderAndFileNum = 0;


            ergodic(NetDisk);

            Height = FolderAndFileNum * 32;

            if (FolderAndFileNum > Controls.Count)
            {
                int ControlsCount = Controls.Count;
                for (int i = 0; i < FolderAndFileNum - ControlsCount; i++)  //少了
                {
                    FileLabel tmp = new FileLabel(this, form1, git_support)
                    {
                        Cursor = Cursors.Hand
                    };
                    Controls.Add(tmp);
                }
            }

            

            FolderAndFileNum = 0;

            if (show1)
            {
                DrawFolder(NetDisk,0);
            }

            for (int i = FolderAndFileNum; i < Controls.Count; i++)
            {
                Panel panel = Controls[i] as Panel;
                panel.Visible = false;
            }
            FolderAndFileNumOld = FolderAndFileNum;
        }

        private void ergodic(string FolderPath)
        {
            DirectoryInfo dir = new DirectoryInfo(FolderPath);
            DirectoryInfo[] subDirs;
            FileInfo[] subFiles;

            try
            {
                subDirs = dir.GetDirectories(); //获取所有子文件夹
                subFiles = dir.GetFiles(); //获取该文件夹中所有文件
            }
            catch (UnauthorizedAccessException)
            {
                return;
            }

            FolderAndFileNum += subDirs.Length;
            FolderAndFileNum += subFiles.Length;

            foreach (DirectoryInfo subDir in subDirs)//文件夹
            {
                foreach (string str in ExpandedFolder)
                {
                    if (str.Equals(subDir.FullName))
                    {
                        ergodic(subDir.FullName);
                        break;
                    }
                }
            }
        }

        private void DrawFolder(string FolderPath,int depth)
        {
            DirectoryInfo dir = new DirectoryInfo(FolderPath);
            DirectoryInfo[] subDirs;
            FileInfo[] subFiles;

            try
            {
                subDirs = dir.GetDirectories(); //获取所有子文件夹
                subFiles = dir.GetFiles(); //获取该文件夹中所有文件
            }
            catch (UnauthorizedAccessException)
            {
                return;
            }
            
            foreach (DirectoryInfo subDir in subDirs)//文件夹
            {
                FileLabel tmp = Controls[FolderAndFileNum] as FileLabel;

                tmp.Name = subDir.Name;
                tmp.FullName = subDir.FullName;
                tmp.BackColor = this.BackColor;
                tmp.Top = FolderAndFileNum * 32;
                tmp.Size = new Size(this.Width, 32);
                tmp.depth = depth;
                tmp.isFolder = true;
                tmp.isUnfold = false;
                FolderAndFileNum++;
                
                foreach (string str in ExpandedFolder)
                {
                    if (str.Equals(subDir.FullName))
                    {
                        tmp.isUnfold = true;
                        DrawFolder(subDir.FullName, depth + 1);
                        break;
                    }
                }
                tmp.AutoHandle();
                tmp.Visible = true;
            }

            foreach (FileInfo subFile in subFiles)//文件
            {
                FileLabel tmp = Controls[FolderAndFileNum] as FileLabel;

                tmp.Name = subFile.Name;
                tmp.FullName = subFile.FullName;
                tmp.BackColor = this.BackColor;
                tmp.Top = FolderAndFileNum * 32;
                tmp.Size = new Size(this.Width, 32);
                tmp.depth = depth;
                tmp.isFolder = false;
                tmp.AutoHandle();
                tmp.Visible = true;

                FolderAndFileNum++;
            }
        }
    }


    public class FileLabel : Panel  //单个文件或者文件夹
    {
        public FileMune UpperStrataPanel;
        public string FullName;
        public Form1 form1;
        private bool git_support = false;
        private int git_supportWidth=0;
        public bool isFolder;
        public bool isUnfold;
        public int depth;
        static int icoWidth = 30;

        string suffix = "";

        private Label nameLable;
        private Label picLable;
        private Color color;

        public FileLabel(FileMune upperStrataPanel, Form1 form1, bool git_support)
        {
            UpperStrataPanel = upperStrataPanel ?? throw new ArgumentNullException(nameof(upperStrataPanel));
            this.form1 = form1 ?? throw new ArgumentNullException(nameof(form1));
            this.git_support = git_support;
            Height = 32;

            picLable = new Label
            {
                AutoSize = false,
                Height = 19,
                Cursor = Cursors.Hand,
            };

            nameLable = new Label
            {
                AutoSize = false,
                Font = new Font("等线", 14F),
                TextAlign = ContentAlignment.MiddleLeft,
                AutoEllipsis = true,
                Height = 19,
                Cursor = Cursors.Hand,
            };

            picLable.MouseDown          += new MouseEventHandler(FileLabel_MouseDown);
            picLable.MouseEnter         += new EventHandler(FileLabel_MouseEnter);
            picLable.MouseLeave         += new EventHandler(FileLabel_MouseLeave);
            picLable.MouseUp            += new MouseEventHandler(FileLabel_MouseUp);
            nameLable.MouseDown         += new MouseEventHandler(FileLabel_MouseDown);
            nameLable.MouseEnter        += new EventHandler(FileLabel_MouseEnter);
            nameLable.MouseLeave        += new EventHandler(FileLabel_MouseLeave);
            nameLable.MouseUp           += new MouseEventHandler(FileLabel_MouseUp);
            MouseDown                   += new MouseEventHandler(FileLabel_MouseDown);
            MouseEnter                  += new EventHandler(FileLabel_MouseEnter);
            MouseLeave                  += new EventHandler(FileLabel_MouseLeave);
            MouseUp                     += new MouseEventHandler(FileLabel_MouseUp);

            Controls.Add(nameLable);
            Controls.Add(picLable);
        }


        public void AutoHandle()
        {
            string[] words = Name.Split('.');
            if (words.Length > 0)
            {
                suffix = words[words.Length - 1];
            }
            else
            {
                suffix = "";
            }
            
            color = Color.FromArgb(201, 209, 217);

            nameLable.BackColor         = BackColor;
            nameLable.ForeColor         = color;  //这里以后要改成  从.git文件夹内部读取信息
            nameLable.Text              = Name;
            nameLable.Location          = new Point(icoWidth + depth * 11, (this.Height - 19) / 2);
            nameLable.Size              = new Size(this.Width - icoWidth - depth * 11 - git_supportWidth, 19);
            picLable.Location           = new Point(depth * 11, (this.Height - icoWidth) / 2);
            picLable.Size               = new Size(icoWidth, icoWidth);
            picLable.BackColor          = BackColor;
            if (isFolder)
            {
                picLable.Image = AllImage.GetFolderIco(isUnfold, picLable.Size, picLable.BackColor);
            }
            else
            {
                picLable.Image = AllImage.GetFileIco(suffix, picLable.BackColor);
            }

            form1.toolTip1.SetToolTip(this, FullName);
            form1.toolTip1.SetToolTip(nameLable, FullName);
        }

        public bool Git_support 
        { 
            get => git_support;
            set 
            { 
                git_support = value;
                if (git_support)
                {
                    git_supportWidth = 30;
                }
                else
                {
                    git_supportWidth = 0;
                }
            }
        }
        
        private Color backcolor;


        private void FileLabel_MouseEnter(object sender, EventArgs e)//进入
        {
            backcolor = this.BackColor;
            BackColor = Color.FromArgb(12, 16, 21);
            
            foreach (var Control in this.Controls)
            {
                ((Label)Control).BackColor = BackColor;  //这里到时候会出错  得加try错误捕获
            }
            if (isFolder)
            {
                picLable.Image = AllImage.GetFolderIco(isUnfold, picLable.Size, picLable.BackColor);
            }
            else
            {
                picLable.Image = AllImage.GetFileIco(suffix, picLable.BackColor);
            }
        }

        private void FileLabel_MouseLeave(object sender, EventArgs e)//离开
        {
            BackColor = backcolor;
            
            foreach (var Control in this.Controls)
            {
                ((Label)Control).BackColor = BackColor;  //这里到时候会出错  得加try错误捕获
            }

            if (isFolder)
            {
                picLable.Image = AllImage.GetFolderIco(isUnfold, picLable.Size, picLable.BackColor);
            }
            else
            {
                picLable.Image = AllImage.GetFileIco(suffix, picLable.BackColor);
            }
        }

        Control control = null;

        private void FileLabel_MouseDown(object sender, MouseEventArgs e)  //按下
        {
            control = sender as Control;
        }

        private void FileLabel_MouseUp(object sender, MouseEventArgs e)  //松开
        {
            if (control != sender as Control) return;

            if (isFolder)
            {
                bool sta = false;

                foreach (string str in UpperStrataPanel.ExpandedFolder)
                {
                    if (str.Equals(FullName))
                    {
                        sta = true;
                    }
                }

                if (sta)
                {
                    UpperStrataPanel.ExpandedFolder.Remove(FullName);
                }
                else
                {
                    UpperStrataPanel.ExpandedFolder.Add(FullName);
                }

                UpperStrataPanel.Draw();

                FileMunePanel fileMunePanel = (FileMunePanel)(UpperStrataPanel.Parent);
                fileMunePanel.flushed();
            }
            else
            {
                
            }

        }
    }


}
